package com.thx.appserver.action;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.appserver.model.CappNews;
import com.thx.appserver.service.CappNewsManager;
import com.thx.common.struts2.CurdAction;
import com.thx.common.web.ActionResult;
import com.thx.file.util.WebFileUtil;


/**
 * 建立日期 : Jul 31, 2012 4:25:39 PM<br>
 * 作者 : 乔红红<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
@Namespace("/appserver")
@Action(value = "news", results = {
		@Result(name = "input",  location = "/com/thx/appserver/news/input.jsp"),
		@Result(name = "list",  location = "/com/thx/appserver/news/list.jsp"),
		@Result(name = "view", location = "/com/thx/appserver/news/detail.jsp"),
		@Result(name = "html5List",location = "/com/thx/appserver/news/html5list.jsp"),
		@Result(name = "html5Content", location = "/com/thx/appserver/news/html5content.jsp"),
		@Result(name = "json", type = "json", params = { "root", "result","contentType", "text/html" })
		},
interceptorRefs ={
		@InterceptorRef("topDtStack"),
		@InterceptorRef(value="pageInterceptor",
		params={"includeMethods","list,doNewHtml5List"})}
)
@Controller
@Scope("prototype")


public class CappNewsAction extends CurdAction<CappNews> {

	private static final long serialVersionUID = 1L;


	private String imguploadFileName;
	private File imgupload;
	private String content;
	public String newsId;




	@Autowired
	private  CappNewsManager newsMgr;
	public CappNewsAction() {
	}

	protected void doList() throws Exception {
		list =newsMgr.searchNews(entity);
	}



	protected void doDelete() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			newsMgr.delete(Long.valueOf(id));
			setResult(ActionResult.SUCCESS, "删除成功！！！");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}
	/**
	 * 取得新闻内容
	 */
	public String doNewHtml5New() {
		if (StringUtils.isNotBlank(newsId)) {
			entity = newsMgr.get(Long.valueOf(newsId));
			if (entity == null) {
				setResult(ActionResult.FAIL, "要查看的记录不存在！！！");
			}
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
		return "html5Content";
	}
	/**
	 * 取得新闻列表
	 */
	public String doNewHtml5List() throws Exception{
	   list=newsMgr.searchNews(entity);
		return "html5List";

	}
	protected void doDelMulti() throws Exception {
		if (delId != null && delId.length > 0) {
			Long[] delIds=new Long[delId.length];
			for(int i=0;i<delId.length;i++){
				Long k=Long.valueOf(delId[i]);
				delIds[i]=k;
			}
			newsMgr.delMulti(delIds);
			setResult(ActionResult.SUCCESS, "删除成功！！！");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}
	/**
	 * 上传图片
	 * @return
	 */
	public String uploadImage() {
		if(imgupload!=null){
			if(StringUtils.isNotBlank(entity.getImageUrl())){
				WebFileUtil.deleteFile(entity.getImageUrl());
			}
	      com.thx.file.model.File file = WebFileUtil.uploadAndSaveFile(imguploadFileName, imgupload);
	      setResult(ActionResult.SUCCESS, file.getFileId());
		}else{
			setResult(ActionResult.FAIL, "图片为空！！！！");
		}

		return "json";

	}
	protected void doInput() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity = newsMgr.get(Long.valueOf(id));
			if (entity == null) {
				setResult(ActionResult.FAIL, "要修改的记录不存在！！！");
			}
		}
	}
	protected void doSave() throws Exception {
		try {
			if (StringUtils.isBlank(id)) {
				System.out.println("内容是："+content);
				System.out.println("内容是："+content.length()+content.getBytes().length);
				setResult(ActionResult.SUCCESS, "添加成功！");
				entity.setCreateDate(new Date());
				entity.setUpdateDate(new Date());

				entity.setContents(content);
			} else {
				setResult(ActionResult.SUCCESS, "修改成功！");
				entity.setNewsId(Long.valueOf(id));
				entity.setUpdateDate(new Date());
			     entity.setContents(content);
			}
			newsMgr.save(entity);
		} catch (Exception e) {
			if (StringUtils.isBlank(id)) {
				setResult(ActionResult.FAIL, "添加失败！");
			} else {
				setResult(ActionResult.FAIL, "修改失败！");
			}
			logger.error(e.getMessage(), e);
		}
	}


	public CappNewsManager getNewsMgr() {
		return newsMgr;
	}

	public void setNewsMgr(CappNewsManager newsMgr) {
		this.newsMgr = newsMgr;
	}

	public String getImguploadFileName() {
		return imguploadFileName;
	}

	public void setImguploadFileName(String imguploadFileName) {
		this.imguploadFileName = imguploadFileName;
	}

	public File getImgupload() {
		return imgupload;
	}

	public void setImgupload(File imgupload) {
		this.imgupload = imgupload;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getNewsId() {
		return newsId;
	}

	public void setNewsId(String newsId) {
		this.newsId = newsId;
	}

}
